<?php

//$site = trim('http://dpol.changyou.com/dpol/news/20130516/59894.shtml?we');
$site = $_GET['site'];
function fs($r, $t) {//无法匹配到时，返回
    $finfo = array('error' => 1, 'title' => $t);
    $fInfo = json_encode($finfo);
    if (!$r) {     
        return $fInfo;
    }
    if (!$r[0]) {
        return $fInfo;
    }
    if (!$r[0][0]) {
        return $fInfo;
    }
}

function str_substr($start, $end, $str) { // 字符串截取函数     
    $temp = explode($start, $str, 2);
    $content = explode($end, $temp[1], 2);
    return $content[0];
}

function curl_file_get_contents($durl) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $durl);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $rs = curl_exec($ch);
    curl_close($ch);
    return $rs;
}

//$buffer = @file_get_contents("http://www.baidu.com/s?wd=" . $site);   
$buffer = @file_get_contents("http://www.baidu.com/s?wd=" . $site); //返回输出文本流
$d = @file_get_contents($site);
if (!$d) {     
    $s = "路径不存在";
    $finfo = array('error' => 1, 'title' => $s);
    $fInfo = json_encode($finfo);
    echo $fInfo;
    return;
} else {
    $s = str_substr("<title>", "</title>", $d);
}
preg_match_all("/<table[^>]+>.+?<\/table>/", $buffer, $r);//匹配表格
$tmp = fs($r, $s);
if ($tmp) {
    echo $tmp;
    return;
}
preg_match_all("/<span[^>]+>.+?(\d{4}-\d+-\d+).+?<\/span>/", $r[0][0], $r);//匹配表格里面的span标签
$tmp = fs($r, $s);
if ($tmp) {
    echo $tmp;
    return;
}
//$txt = '<span class="g">  <b>xsh.changyou.com</b>/<b>dhsh</b>/<b>events</b>/events... 2013-1-27  </span>';
//echo $txt;
preg_match_all("/(\d{4}-\d+-\d+)/", $r[0][0], $r);//匹配日期
$tmp = fs($r, $s);
if ($tmp) {    
    echo $tmp;
    return;
}
$info = array('error' => 0, 'date' => $r[0][0], 'title' => $s);
$userinfo = json_encode($info);
echo $userinfo;
?>